package demo4;

import java.util.Arrays;

class Dog implements Comparable<Dog> {
    private String name;
    private int age;

    @Override
    public int compareTo(Dog o) {
        // 拿当前对象减传进来的对象是升序排序
//        return this.age - o.age;
//        return Integer.compare(this.age, o.age);
        return this.age > o.age ? 1 : (this.age == o.age ? 0 : -1);
    }

    public Dog() {
    }

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


}

public class Test_sort {
    public static void main(String[] args) {
        int[] arr1 = {8,18,15,24,7,55,22};
        // 冒泡排序
        for (int j = 0; j < arr1.length; j++) {
            for (int i = 0; i < arr1.length - 1 - j; i++) {
                if (arr1[i] > arr1[i + 1]){
                    int temp;
                    temp = arr1[i];
                    arr1[i] = arr1[i + 1];
                    arr1[i + 1] = temp;
                }
            }
        }

//        System.out.println(Arrays.toString(arr1));

        Dog[] dogs = {  new Dog("小白",5),
                        new Dog("小黑",3),
                        new Dog("旺财",28),
                        new Dog("富贵",18)
        };
//        Arrays.sort(dogs);

        for (int j = 0; j < dogs.length; j++) {
            for (int i = 0; i < dogs.length - 1 - j; i++) {
                if (dogs[i].compareTo(dogs[i + 1]) > 0){
                    Dog temp;
                    temp = dogs[i];
                    dogs[i] = dogs[i + 1];
                    dogs[i + 1] = temp;
                }
            }
        }

        System.out.println(Arrays.toString(dogs));
    }
}
